package lt.marsrutai.mobile.android.fragment;

import java.util.List;

import lt.marsrutai.mobile.android.AppContext;
import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.StopTimesIntentHolder;
import lt.marsrutai.mobile.android.activity.intent.StopsIntentHolder;
import lt.marsrutai.mobile.android.adapter.StopAdapter;
import lt.marsrutai.mobile.android.exception.DataAccessException;
import lt.marsrutai.mobile.android.manager.ActivityManager;
import lt.marsrutai.mobile.android.model.Stop;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;


public class StopsFragment extends SherlockListFragment {

    private ActivityManager manager;

    private List<Stop> stops;
    private ArrayAdapter<Stop> adapter;
    private Runnable downloadTask;

    private String scopeFolder;
    private String scopeName;
    private String transportId;
    private String scheduleId;
    private String scheduleName;
    private String scheduleTrackName;
    private String trackId;
    private String trackName;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.list_fragment, null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        manager = new ActivityManager(getActivity());
        manager.showDownloadLoadingBar();

        receiveItemData();

        adapter = new StopAdapter(getActivity(), scopeFolder, trackId);
        setListAdapter(adapter);

        downloadData();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        sendItemData(position);
    }

    public void receiveItemData() {
        StopsIntentHolder intentHolder = new StopsIntentHolder(getArguments());
        scopeFolder = intentHolder.getScopeFolder();
        scopeName = intentHolder.getScopeName();
        transportId = intentHolder.getTransportId();
        scheduleId = intentHolder.getScheduleId();
        scheduleName = intentHolder.getScheduleName();
        scheduleTrackName = intentHolder.getScheduleTrackName();
        trackId = intentHolder.getTrackId();
        trackName = intentHolder.getTrackName();
    }

    public void sendItemData(int i) {
        Stop stop = stops.get(i);
        StopTimesIntentHolder intentHolder = new StopTimesIntentHolder(getActivity());
        intentHolder.setScopeFolder(scopeFolder);
        intentHolder.setScopeName(scopeName);
        intentHolder.setTransportId(transportId);
        intentHolder.setScheduleId(scheduleId);
        intentHolder.setScheduleName(scheduleName);
        intentHolder.setScheduleTrackName(scheduleTrackName);
        intentHolder.setTrackId(trackId);
        intentHolder.setTrackName(trackName);
        intentHolder.setStopId(stop.getId());
        intentHolder.setStopName(stop.getName());
        intentHolder.setStopDirectionName(stop.getDirectionName());
        startActivity(intentHolder.getIntent());
    }

    public void downloadData() {
        downloadTask = new Runnable() {

            @Override
            public void run() {
                getStops(scopeFolder);
            }
        };
        Thread thread = new Thread(downloadTask);
        thread.start();
    }

    private void getStops(String scopeName) {
        try {
            stops = AppContext.getDataService().getTrackStops(scopeName, trackId);
        }
        catch (DataAccessException e) {
            manager.runOnUiThread(manager.errorRes);
            return;
        }
        manager.runOnUiThread(returnRes);
    }

    private final Runnable returnRes = new Runnable() {

        @Override
        public void run() {
            if (stops != null && stops.size() > 0) {
                for (int i = 0; i < stops.size(); i++) {
                    adapter.add(stops.get(i));
                }
            }
            manager.dismissDownloadLoadingBar();
        }
    };

}
